package offer2

/**
原题链接:
https://leetcode.cn/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/

B站小美算法
剑指offer 39.数组中出现次数超过一半的数字
*/

func majorityElement(nums []int) int {
	target, count := 0, 0
	for i := 0; i < len(nums); i++ { //遍历数组
		if count == 0 { //如果计数为0,target = nums[i],计数++
			target = nums[i]
			count++
		} else { //如果count不为0了,
			if target != nums[i] {
				count--
			} else {
				count++
			}
		}
	}
	return target
}
